package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.app.domain.AppRecord;
import com.ruoyi.app.domain.bo.AppDynamicLikeBo;
import com.ruoyi.app.domain.vo.AppDynamicLikeVo;
import com.ruoyi.app.domain.vo.AppDynamicVo;
import com.ruoyi.app.domain.vo.AppRecordVo;
import com.ruoyi.app.mapper.AppRecordMapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.enums.NoticType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppStoryLikeBo;
import com.ruoyi.app.domain.vo.AppStoryLikeVo;
import com.ruoyi.app.domain.AppStoryLike;
import com.ruoyi.app.mapper.AppStoryLikeMapper;
import com.ruoyi.app.service.IAppStoryLikeService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * APP用户故事墙点赞Service业务层处理
 *
 * @author heike
 * @date 2025-03-22
 */
@RequiredArgsConstructor
@Service
public class AppStoryLikeServiceImpl implements IAppStoryLikeService {

    private final AppStoryLikeMapper baseMapper;
    private final AppRecordMapper appRecordMapper;

    /**
     * 查询APP用户故事墙点赞
     */
    @Override
    public AppStoryLikeVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询APP用户故事墙点赞
     */
    @Override
    public AppStoryLikeVo queryByStoryIdAndUserId(Long id, Long userId) {
        LambdaQueryWrapper<AppStoryLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppStoryLike::getUserId, userId).eq(AppStoryLike::getStoryId, id).eq(AppStoryLike::getType, Constants.FAIL);
        return baseMapper.selectVoOne(wrapper);
    }

    /**
     * 查询APP用户故事墙点赞列表
     */
    @Override
    public TableDataInfo<AppStoryLikeVo> queryPageList(AppStoryLikeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppStoryLike> lqw = buildQueryWrapper(bo);
        Page<AppStoryLikeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询APP用户故事墙点赞列表
     */
    @Override
    public List<AppStoryLikeVo> queryList(AppStoryLikeBo bo) {
        LambdaQueryWrapper<AppStoryLike> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppStoryLike> buildQueryWrapper(AppStoryLikeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppStoryLike> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStoryId() != null, AppStoryLike::getStoryId, bo.getStoryId());
        lqw.eq(bo.getUserId() != null, AppStoryLike::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), AppStoryLike::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增APP用户故事墙点赞
     */
    @Override
    public Boolean insertByBo(AppStoryLikeBo bo) {
        AppStoryLike add = BeanUtil.toBean(bo, AppStoryLike.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP用户故事墙点赞
     */
    @Override
    public Boolean updateByBo(AppStoryLikeBo bo) {
        AppStoryLike update = BeanUtil.toBean(bo, AppStoryLike.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppStoryLike entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP用户故事墙点赞
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 点赞和取消点赞
     *
     * @param id          记录ID
     * @param userId      当前用户ID
     * @param storeUserId 故事墙发布用户ID
     * @return
     */
    @Override
    @Transactional
    public String addLike(Long id, Long userId, Long storeUserId) {
        String result = Constants.TWO;
        AppStoryLikeVo storyLikeVo = queryByStoryIdAndUserId(id, userId);
        if (Objects.isNull(storyLikeVo)) {
            AppStoryLikeBo storyLikeBo = new AppStoryLikeBo();
            storyLikeBo.setStoryId(id);
            storyLikeBo.setUserId(userId);
            storyLikeBo.setType(Constants.FAIL);
            //添加动态点赞通知记录
            if (insertByBo(storyLikeBo)) {
                AppRecord record = new AppRecord();
                record.setRecordId(id);
                record.setUserId(userId);
                record.setRecordUserId(storeUserId);
                record.setType(NoticType.LIKE.getCode());
                record.setReadStatus(Constants.SUCCESS);
                result = Constants.SUCCESS;
            }
        } else {
            LambdaQueryWrapper<AppRecord> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AppRecord::getRecordId, id).eq(AppRecord::getUserId, userId).eq(AppRecord::getType, NoticType.LIKE.getCode());
            AppRecordVo appRecordVo = appRecordMapper.selectVoOne(wrapper);
            if (Objects.nonNull(appRecordVo)) {
                appRecordMapper.deleteById(appRecordVo.getId());
            }
            baseMapper.deleteById(storyLikeVo.getId());
            result = Constants.FAIL;
        }
        return result;
    }
}
